#include "Cotan.h"
#include "Putere.h"
#include "Cos.h"

long double Cotan::calcul(long double aux1)
{
    return Impartit::calcul(Cos::calcul(aux1), Sin::calcul(aux1));
}

long double Cotan::calcul()
{
    if (!fStang)
        exit(1);

    return calcul(fStang->calcul());
}

char *Cotan::derivare()
{
    char *aux1, *aux2, *aux, *aux00, *aux01, *aux02;

    if (!fStang)
        exit(1);

    aux1 = fStang->derivare();
    aux2 = fStang->toString();
    aux00 = Sin::concatTermeni(aux2);
    aux01 = Putere::concatTermeni(aux00,(char*) "2");
    aux02 = Minus::concatTermeni(0, aux1);
    aux = Impartit::concatTermeni(aux02, aux01);

    delete aux1; delete aux2; delete aux00; delete aux01; delete aux02;
    return aux;
}

long double Cotan::calculDerivata()
{
    if (!fStang)
        exit(1);

    return Minus::calcul(0, Inmultit::calcul(fStang->calculDerivata(),
                Putere::calcul(Sin::calcul(fStang->calcul()), 0.5)));
}

char *Cotan::toString()
{
    char *aux, *aux1;
    int nrP;

    if (!fStang)
        exit(1);
    aux1 = fStang->toString();

    aux = concatTermeni(aux1);
    delete aux1;

    return aux;
}

char *Cotan::concatTermeni(char *aux1)
{
    char *aux;
    int nrP;

    nrP = aux1[0] == '(' ? 0 : 2;
    aux = new char[strlen(aux1) + 4 + nrP];

    strcpy(aux, "ctg");
    if (nrP) strcat(aux, "(");
    strcat(aux, aux1);
    if (nrP) strcat(aux, ")");

    return aux;
}
